import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_model_package/CourseClass.dart';
import 'package:flutter_model_package/CourseLesson.dart';
import 'calendar_page_viewModel.dart';
import 'calendar_widget.dart';
import 'day.dart';
import 'package:collection/collection.dart';
enum Location { left, mid, right }

typedef void SelectDateOnTap(
    DayModel checkInTimeModel, DayModel leaveTimeModel);

class CalendarPage extends StatefulWidget {
  final CourseClass courseClass;

  const CalendarPage({Key key, this.courseClass}) : super(key: key);
  @override
  _CalendarPageState createState() => _CalendarPageState();
}

class _CalendarPageState extends State<CalendarPage> {
  String _selectCheckInTime = '';

  List<CalendarItemViewModel> _list = [];

  @override
  void initState() {
    super.initState();
    // 加载日历数据源
    _list = CalendarViewModel().getItemList();
    getStuLesson();
  }

  Map events={};

  Future<void> getStuLesson()async{
    var rep = await CourseLesson.getstuLesson(widget.courseClass);
    List<CourseLesson> list = [];
    list = rep.map((e) => e).toList();
    setState(() {
      events = groupBy(list,(obj)=>formatDate(obj['date'],[yyyy,'-',mm,'-',dd]));
      print(list);
      print(events);
    });
  }


  @override
  Widget build(BuildContext context) {
    final data = MediaQuery.of(context);
    // 屏幕宽高
    double screenWith = MediaQuery.of(context).size.width;
    final screenHeight = data.size.height;
    final screenWidth = data.size.width;
    return Scaffold(
      body: Container(
        color: Colors.white,
        width: double.maxFinite,
        height: screenHeight,
        child: Stack(
          children: <Widget>[
            Column(
              children: <Widget>[
                _weekItem(screenWith),
                SizedBox(
                  width: 20,
                ),

                // 月日期的视图
                Expanded(
                  child: ListView.builder(
                    itemBuilder: (BuildContext context, int index) {
                      CalendarItemViewModel itemModel = _list[index];
                      return CalendarItem((year, month, checkInTime) {
                        _updateCheckInLeaveTime(year, month, checkInTime);
                      }, itemModel,events);
                    },
                    itemCount: _list.length,
                  ),
                ),
              ],
            ),
            Positioned(
              left: 0,
              right: 0,
              bottom: 0,
              height: MediaQuery.of(context).padding.bottom,
              child: Container(),
            ),
          ],
        ),
      ),
    );
  }

  /*
  * 比较后面的日期是比model日期小（left） 还是相等(mid) 还是大 (right)
  * */
  _comparerTime(DayModel model, int year, int month, int day) {
    if (year > model.year) {
      return Location.right;
    } else if (year == model.year) {
      if (model.month < month) {
        return Location.right;
      } else if (model.month == month) {
        if (model.dayNum < day) {
          return Location.right;
        } else if (model.dayNum == day) {
          return Location.mid;
        } else {
          return Location.left;
        }
      } else {
        return Location.right;
      }
    } else {
      return Location.left;
    }
  }

  _weekItem(double screenW) {
    List<String> _listS = <String>[
      '日',
      '一',
      '二',
      '三',
      '四',
      '五',
      '六',
    ];
    List<Widget> _listW = [];
    _listS.forEach((title) {
      _listW.add(_weekTitleItem(title, (screenW - 40) / 7));
    });
    return Container(
      width: screenW - 40,
      height: 17,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: _listW,
      ),
    );
  }

  /*
  * 周内对应的每天的组件
  * */
  _weekTitleItem(String title, double width) {
    return Container(
      alignment: Alignment.center,
      width: width,
      child: Text(
        title,
        style: TextStyle(
          color: Color(0xff2D7FC7),
          fontSize: 12,
          fontWeight: FontWeight.w500,
          fontFamily: 'PingFangSC-Semibold',
        ),
      ),
    );
  }

  /*
  * 更新日历的数据源
  * */
  _updateDataSource(int year, int month, int checkInTime) {
    DayModel firstModel;
    for (int i = 0; i < _list.length; i++) {
      CalendarItemViewModel model = _list[i];
      if (model.firstSelectModel != null) {
        firstModel = model.firstSelectModel;
      }
    }

    if (firstModel != null) {
      for (int i = 0; i < _list.length; i++) {
        CalendarItemViewModel model = _list[i];
        model.firstSelectModel = null;
        model.lastSelectModel = null;
        firstModel = null;
        for (int i = 0; i < model.list.length; i++) {
          DayModel dayModel = model.list[i];
          dayModel.isSelect = false;
          if (_comparerTime(dayModel, year, month, checkInTime) ==
              Location.mid) {
            dayModel.isSelect = true;
            model.firstSelectModel = dayModel;
            _selectCheckInTime =
                '$year-${Day.day(month)}-${Day.day(checkInTime)}';
          }
        }
      }
    } else if (firstModel != null) {
      if (_comparerTime(firstModel, year, month, checkInTime) ==
          Location.left) {
        for (int i = 0; i < _list.length; i++) {
          CalendarItemViewModel model = _list[i];
          model.firstSelectModel = null;
          model.lastSelectModel = null;
          firstModel = null;
          for (int i = 0; i < model.list.length; i++) {
            DayModel dayModel = model.list[i];
            dayModel.isSelect = false;
            if (_comparerTime(dayModel, year, month, checkInTime) ==
                Location.mid) {
              dayModel.isSelect = !dayModel.isSelect;
              model.firstSelectModel = dayModel;
              _selectCheckInTime =
                  '$year-${Day.day(month)}-${Day.day(checkInTime)}';
            }
          }
        }
      } else if (_comparerTime(firstModel, year, month, checkInTime) ==
          Location.mid) {
        //点击了自己
        for (int i = 0; i < _list.length; i++) {
          CalendarItemViewModel model = _list[i];
          model.lastSelectModel = null;
          if (model.month == month) {
            for (int i = 0; i < model.list.length; i++) {
              DayModel dayModel = model.list[i];
              if (_comparerTime(dayModel, year, month, checkInTime) ==
                  Location.mid) {
                dayModel.isSelect = !dayModel.isSelect;
                model.firstSelectModel = dayModel.isSelect ? dayModel : null;
                _selectCheckInTime = dayModel.isSelect
                    ? '$year-${Day.day(month)}-${Day.day(checkInTime)}'
                    : '';
              }
            }
          }
        }
      }
    } else if (firstModel == null) {
      for (int i = 0; i < _list.length; i++) {
        CalendarItemViewModel model = _list[i];
        model.firstSelectModel = null;
        model.lastSelectModel = null;
        firstModel = null;
        for (int i = 0; i < model.list.length; i++) {
          DayModel dayModel = model.list[i];
          dayModel.isSelect = false;
          if (_comparerTime(dayModel, year, month, checkInTime) ==
              Location.mid) {
            dayModel.isSelect = true;
            model.firstSelectModel = dayModel;
            _selectCheckInTime =
                '$year-${Day.day(month)}-${Day.day(checkInTime)}';
          }
        }
      }
    }
  }

  /*
  * 点击日期的回调事件
  * */
  _updateCheckInLeaveTime(int year, int month, int checkInTime) {
    print(checkInTime);
    // 更新数据源
    _updateDataSource(year, month, checkInTime);
    // 刷新UI
    setState(() {});
  }
}
